<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in commentList"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate |fromNow }}</span>
            <van-button size="mini" type="default" @click="reply(item)">回复{{ item.reply_count }}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" :name="item.is_liking?'like':'like-o'" @click.stop="tag(item)" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model.trim="commentContext"
        clearable
        placeholder="请输入评论内容"
      >
        <van-button slot="button" size="mini" type="info" @click="post()">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /回复评论 -->
    <van-action-sheet v-if="isShowReply" v-model="isShowReply" title="标题" style="height:60%;">
      <CommentReply :replyitem="replyItem" @addReplyCount="addReplyCount" />
    </van-action-sheet>
  </div>
</template>

<script>
import { reqGetComment, reqAddComment, reqCommentTag, reqCancelCommentTag } from '@/api/article'
import CommentReply from '@/views/article/commentReply.vue'
// import { reqGetComment } from '@/api/article'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  props: {},
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      commentList: [],
      commentContext: '',
      isShowReply: false,
      replyItem: ''

    }
  },
  computed: {
    articleId() {
      return this.$route.params.id
    }
  },

  methods: {
    async onLoad() {
      //  const res =await reqGetComment('a',this.$route.params.id,)
      const res = await reqGetComment('a', this.articleId, this.offset)
      this.offset = res.data.last_id
      this.commentList = [...this.commentList, ...res.data.results]
      this.loading = false
      if (this.commentList.length === 0 || this.commentList.length > 50 || !res.data.last_id) {
        this.finished = true
      }
    },
    async post() {
      if (!this.commentContext) return this.$toast.fail('内容为空')
      const res = await reqAddComment(this.articleId, this.commentContext)
      this.commentList.unshift(res.data.new_obj)
      this.commentContext = ''
    },
    async tag(item) {
      console.log(item)
      if (!this.$store.state.user.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: 'true'
          }
        })
        this.$toast.fail('请先登录账号')
        return
      }
      if (!item.is_liking) {
        await reqCommentTag(item.com_id)
        this.$toast.success('点赞成功')
      } else {
        await reqCancelCommentTag(item.com_id)
        this.$toast.success('取消成功')
      }
      item.is_liking = !item.is_liking
      this.$forceUpdate()
    },
    reply(item) {
      this.isShowReply = true
      this.replyItem = item
    },
    addReplyCount() {
      this.replyItem.reply_count++
    }

  }

}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
